#include "OptionsState.h"
#include <fstream>
#include "../SGD Wrappers/SGD_InputManager.h"
#include "../SGD Wrappers/SGD_AudioManager.h"
#include "Game.h"
#include "MainMenuState.h"
#include "BitmapFont.h"
#include "Game.h"

OptionsState* OptionsState::GetInstance()
{
	static OptionsState s_Instance;
	return &s_Instance;
}

void OptionsState::Enter()
{
	m_nCursor = 0;
	m_nNumItems = 0;
	m_fTimer = 0;

	Game::Save* save = Game::GetInstance()->GetSave();

	m_hBackgroundImage = SGD::GraphicsManager::GetInstance()->LoadTexture("resource/graphics/Large_Frame_Purple.png");

	std::fstream file;
	file.open("resource/menu/Options.mnu", std::ios_base::in);

	if (file.is_open())
	{
		file >> m_nNumItems;
		file.ignore(INT_MAX, '\n');
		for (unsigned char i = 0; i < m_nNumItems; i++)
		{
			char szTemp[100];
			file.getline(&szTemp[0], 100, '\n');

			SGD::String strItem;
			for (unsigned char i = 0; i < 100; i++)
			{
				strItem += szTemp[i];
				if (szTemp[i] == '\0')
				{
					//strItem += szTemp[i];
					break;
				}
			}
			m_szItems.push_back(strItem);
		}

		m_bFullScreen = save->FullScreen;
		m_nMusicVolume = save->MusicVolume;
		m_SFXVolume = save->SFXVolume;
		m_szItems[0] += std::to_wstring( (int)(save->MusicVolume));
		m_szItems[1] += std::to_wstring((int)(save->SFXVolume));
	}
	file.close();

	m_haBackground	= SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Choose_Your_Phat.xwm");
	m_haSFX			= SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Scream.wav");
}

void OptionsState::Exit()
{

	// Save settings back to file
	std::fstream file;
	file.open("resource/menu/Options.mnu", std::ios_base::out);

	if (file.is_open())
	{
		file << m_nNumItems <<'\n';
		for (unsigned char i = 0; i < m_nNumItems; i++)
		{
			int parse = m_szItems[i].find_last_of((wchar_t)0);
			m_szItems[i].erase(m_szItems[i].begin() + parse + 1, m_szItems[i].end());
			
			for (unsigned int j = 0; j < m_szItems.at(i).size(); j++)
				file << (char)m_szItems.at(i).at(j);
			file <<'\n';
		}
		//if (m_bFullScreen)
		//	file << "Yes\n";
		//else
		//	file << "No\n";
		//
		//file << m_nMusicVolume<<'\n';
		//file << m_SFXVolume<<'\n';
	}
	file.close();

	Game::Save* save = Game::GetInstance()->GetSave();
	save->FullScreen = m_bFullScreen;
	save->MusicVolume = m_nMusicVolume;
	save->SFXVolume = m_SFXVolume;

	SGD::AudioManager::GetInstance()->UnloadAudio(m_haBackground);
	SGD::AudioManager::GetInstance()->UnloadAudio(m_haSFX);

	Game* game = Game::GetInstance();
	SGD::Size resize = { (float)game->GetScreenWidth(), (float)game->GetScreenHeight() };

	SGD::GraphicsManager::GetInstance()->Resize(resize, m_bFullScreen);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_hBackgroundImage);
}

void OptionsState::Update(float dt)
{
	m_fTimer += dt;

	// Parse SFX out and update
	int parse = m_szItems[0].find_last_of((wchar_t)0);
	m_szItems[0].erase(m_szItems[0].begin() + parse+1, m_szItems[0].end());
	m_szItems[0] += std::to_wstring((int)(m_nMusicVolume ));

	// Parse SFX out and update
	parse = m_szItems[1].find_last_of((wchar_t)0);
	m_szItems[1].erase(m_szItems[1].begin() + parse+1, m_szItems[1].end());
	m_szItems[1] += std::to_wstring((int)(m_SFXVolume ));

	parse = m_szItems[2].find_last_of((wchar_t)0);
	if ((unsigned int)(parse + 1) < m_szItems[2].size())
		m_szItems[2].erase(m_szItems[2].begin() + parse + 1, m_szItems[2].end());

		if (!m_bFullScreen)
			m_szItems[2] += L"Yes";
		else
			m_szItems[2] += L"No";

		SGD::AudioManager* audio = SGD::AudioManager::GetInstance();

		if (m_nCursor == 0 && !audio->IsAudioPlaying(m_haBackground))
			audio->PlayAudio(m_haBackground, true);
		else if (m_nCursor != 0 && audio->IsAudioPlaying(m_haBackground))
			audio->StopAudio(m_haBackground);

		if (m_nCursor == 1 && !audio->IsAudioPlaying(m_haSFX))
			audio->PlayAudio(m_haSFX, true);
		else if (m_nCursor != 1 && audio->IsAudioPlaying(m_haSFX))
			audio->StopAudio(m_haSFX);
}

void OptionsState::Render()
{
	BitmapFont temp;
	temp.Initialize();
	Game* game = Game::GetInstance();
	SGD::GraphicsManager::GetInstance()->DrawTexture(m_hBackgroundImage, { (game->GetScreenWidth() / 2.f) - 256, (game->GetScreenHeight() / 2.f) - 256 });

	// Draw each item to screen
	for(unsigned int i = 0; i < m_nNumItems; i++)
	{
		if (m_nCursor == i)
			temp.Draw(&m_szItems[i],   50 * 6, 200 +  i * 60, 0.75, SGD::Color(255, 255, 0));
		else
			temp.Draw(&m_szItems[i],  50 * 5, 200 +  i * 60, 0.5f, SGD::Color());
	}
}

bool OptionsState::Input()
{
	SGD::InputManager* input = SGD::InputManager::GetInstance();
	SGD::Vector controller;

	SGD::AudioManager* audio = SGD::AudioManager::GetInstance();

	if (m_fTimer > 0.15f)
	{
		controller = input->GetLeftJoystick(0);
		m_fTimer = 0.f;
	}

	if (input->IsKeyPressed(SGD::Key::Enter) || input->IsButtonPressed(0, 0))
		switch (m_nCursor)
	{
		case 2:
		{
					 m_bFullScreen = !m_bFullScreen;
				 break;
		}
		case 3:
		{
				  Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
		}
		default:
			break;
	}

	if (input->IsKeyPressed(SGD::Key::W)
		|| input->IsKeyPressed(SGD::Key::UpArrow)
		|| controller.y < -0.2f)
	{
		--m_nCursor;

		// Stop at top
		if (m_nCursor < 0)
			m_nCursor = 0;
	}

	if (input->IsKeyPressed(SGD::Key::S)
		|| input->IsKeyPressed(SGD::Key::DownArrow)
		|| controller.y > 0.2f)
	{
		++m_nCursor;

		// Stop at the bottom
		if (m_nCursor >(int) m_nNumItems - 1)
			m_nCursor = m_nNumItems - 1;
	}

	if (input->IsKeyPressed(SGD::Key::D) 
		||input->IsKeyPressed(SGD::Key::RightArrow)
		|| controller.x > 0.2f)
	{
		if (m_nCursor == 0 && m_nMusicVolume < 100)
		{
			m_nMusicVolume += 5;
				audio->SetMasterVolume(SGD::AudioGroup::Music, m_nMusicVolume);
		}

		if (m_nCursor == 1 && m_SFXVolume < 100)
		{
			m_SFXVolume += 5;
				audio->SetMasterVolume(SGD::AudioGroup::SoundEffects, m_SFXVolume);
		}

		if (m_nCursor == 2)
			m_bFullScreen = !m_bFullScreen;
	}

	if (input->IsKeyPressed(SGD::Key::A) 
		|| input->IsKeyPressed(SGD::Key::LeftArrow)
		|| controller.x < -0.2f)
	{
		if (m_nCursor == 0 && m_nMusicVolume > 0)
		{
			m_nMusicVolume -= 5;
				audio->SetMasterVolume(SGD::AudioGroup::Music, m_nMusicVolume);
		}

		if (m_nCursor == 1 && m_SFXVolume > 0)
		{
			m_SFXVolume -= 5;
				audio->SetMasterVolume(SGD::AudioGroup::SoundEffects, m_SFXVolume);
		}

		if (m_nCursor == 2)
			m_bFullScreen = !m_bFullScreen;
	}

	// Esc sets cursor to exit
	if (input->IsKeyPressed(SGD::Key::Escape)
		|| input->IsButtonPressed(0, 1))
		m_nCursor = m_nNumItems - 1;

	return true;
}